class Solution {
public:
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    int ret;
    bool check[101][101]{false};
    int m,n;
    int wardrobeFinishing(int _m, int _n, int c) {
        m=_m;
        n=_n;
        dfs(0,0,c);
        return ret;
    }

    void dfs(int i,int j,int c)
    {
        check[i][j]=true;
        ret++;
        for(int k=0;k<4;++k)
        {
            int x=dx[k]+i;
            int y=dy[k]+j;
            if(x>=0&&y>=0&&x<m&&y<n&&
            (com(x,y)<=c)&&check[x][y]==false)
            {
                
                dfs(x,y,c);
            }
        }
    }

    int com(int x,int y)
    {
        int tmp=0;
        while(x)
        {
            tmp+=x%10;
            x/=10;
        }
        while(y)
        {
            tmp+=y%10;
            y/=10;
        }
        return tmp;
    }
};
